package com.hejielife.twophasetermination1;

/**
 * 2019年8月2日 下午4:45:21
 * 
 * @author hejielife
 * @despribe CountupThread
 */
public class CountupThread extends Thread {
	// counter
	private long counter = 0;

	// shutdownRequested
	private volatile boolean shutdownRequested = false;

	public void shutdownRequest() {
		shutdownRequested = true;
		interrupt();
	}

	public boolean isShutdownRequested() {
		return shutdownRequested;
	}

	public final void run() {
		try {
			while (!shutdownRequested) {
				doWork();
			}
		} catch (InterruptedException e) {
		} finally {
			doShutdown();
		}
	}

	// dowork
	private void doWork() throws InterruptedException {
		counter++;
		System.out.println("doWork: counter = " + counter);
		Thread.sleep(500);
	}

	// 结束
	private void doShutdown() {
		System.out.println("doShutdown: counter = " + counter);
	}
}
